package cn.wit.summit.chwan.utils;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;

import java.io.File;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.onekeyshare.ShareContentCustomizeCallback;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;
import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.Stat.StatFactory;
import cn.wit.summit.chwan.models.dto.ShareBean;
import cn.wit.summit.chwan.models.dto.ShareBeanMain;

/**
 * Created by hezl on 2015/1/26.
 */
public class CommonUtil {

    private static String TAG = "CommonUtil";

    public static String getPackageName(Context context) {
        PackageManager mPackageManager = context.getPackageManager();
        String mPackageName = "";
        try {
            PackageInfo pInfo = mPackageManager.getPackageInfo(context.getPackageName(), 0);
            mPackageName = pInfo.packageName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return mPackageName;
    }

    //返回一个安装到设备上的所有应用程序包列表
    public static String getPackageSize(Context context, String mPackageName) {
        List<ApplicationInfo> installList = context.getPackageManager().getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
        BigDecimal apkSize = null;
        for (int i = 0; i < installList.size(); i++) {
            ApplicationInfo info = installList.get(i);
            if (info.packageName.equals(mPackageName)) {//如果这个packagename和当前apk的packagename一样，你就可以获取它的大小了。
                String dir = info.publicSourceDir;
                int size = Integer.valueOf((int) new File(dir).length());
                BigDecimal bd = new BigDecimal((double) size / (1024 * 1024));
                apkSize = bd.setScale(2, BigDecimal.ROUND_DOWN);
            }
        }
        return apkSize + "";
    }







    /**
     * 分享
     *
     * @param context
     */
    public static void shareUtil(Context context, String json, int from) {
        try {
            ShareBeanMain shareBeanMain = JsonMapper.getInstance().fromJson(json, ShareBeanMain.class);
            final ShareBean shareBean = shareBeanMain.getShare();
            if (shareBean != null) {
                shareBean.setFrom(from);
                Log.v("infoo", shareBean.toString());
            }
            shareUtil(context, shareBean);
        } catch (Exception e) {
            e.printStackTrace();
            ToastUtils.getInstance(context).showToastSystem("分享失败。");
        }
    }

    private  static  void showShare(Context context) {
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();

        // title标题，微信、QQ和QQ空间等平台使用
        oks.setTitle("标题");
        // titleUrl QQ和QQ空间跳转链接
        oks.setTitleUrl("http://sharesdk.cn");
        // text是分享文本，所有平台都需要这个字段
        oks.setText("我是分享文本");
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url在微信、微博，Facebook等平台中使用
        oks.setUrl("http://sharesdk.cn");
        // comment是我对这条分享的评论，仅在人人网使用
        oks.setComment("我是测试评论文本");
        // 启动分享GUI
        oks.show(context);
    }

    /**
     * 分享
     *
     * @param context
     */
    public static void shareUtil(final Context context, final ShareBean shareBean) {
        try {
//            if (true)
//            showShare(context);

            final String uid = AccountUtil.getInstance().getUid()+"";
            final int from = shareBean.getFrom();
//            ShareSDK.initSDK(context);
            OnekeyShare oks = new OnekeyShare();
//            oks.setDialogMode();

//            oks.setTopIntentData(shareBean.getIntentData());
            if (shareBean.getTitle()==null){
                shareBean.setTitle("一起玩");
            }
            if (shareBean.getText()==null){
                shareBean.setText("一起来玩游戏吧");
            }
            oks.setTitle(shareBean.getTitle());
            oks.setText(shareBean.getText());
            //关闭sso授权
            oks.disableSSOWhenAuthorize();
            final String imageUrl = shareBean.getImageUrl();
            if (StringUtils.isNotEmpty(imageUrl))
                oks.setImageUrl(imageUrl);
            // url仅在微信（包括好友和朋友圈）中使用
            String weichaurl = shareBean.getWechatShareUrl();
            if (StringUtils.isNotEmpty(weichaurl)) {
                if (weichaurl.contains("?")) {
                    weichaurl = weichaurl + "&t=" + System.currentTimeMillis();
                } else {
                    weichaurl = weichaurl + "?t=" + System.currentTimeMillis();
                }
                oks.setUrl(weichaurl);
            }
            // comment是我对这条分享的评论，仅在人人网和QQ空间使用
            oks.setComment(shareBean.getText());
            // site是分享此内容的网站名称，仅在QQ空间使用
            oks.setSite(context.getString(R.string.app_name));
            // siteUrl是分享此内容的网站地址，仅在QQ空间使用
            oks.setShareContentCustomizeCallback(new ShareContentCustomizeCallback() {
                @Override
                public void onShare(
                        Platform platform, Platform.ShareParams paramsToShare) {
                    if (SinaWeibo.NAME.equals(platform.getName())) {
                        String weibourl = shareBean.getWeiboShareUrl();
                        if (StringUtils.isNotEmpty(weibourl)) {
                            if (weibourl.contains("?")) {
                                weibourl = weibourl + "&t=" + System.currentTimeMillis();
                            } else {
                                weibourl = weibourl + "?t=" + System.currentTimeMillis();
                            }
                            paramsToShare.setTitleUrl(weibourl);
                        }
                        paramsToShare.setText(shareBean.getText() + "\n" + shareBean.getWeiboShareUrl());
                    }
                    if (QZone.NAME.equals(platform.getName())) {
                        String qqzoneurl = shareBean.getqZoneShareUrl();
                        if (StringUtils.isNotEmpty(qqzoneurl)) {
                            if (qqzoneurl.contains("?")) {
                                qqzoneurl = qqzoneurl + "&t=" + System.currentTimeMillis();
                            } else {
                                qqzoneurl = qqzoneurl + "?t=" + System.currentTimeMillis();
                            }
                            paramsToShare.setTitleUrl(qqzoneurl);
                        }
                        String ImageUrl = shareBean.getImageUrl();
                        if (StringUtils.isNotEmpty(ImageUrl))
                            paramsToShare.setImageUrl(ImageUrl);
                    }

                    //qq
                    if (QQ.NAME.equals(platform.getName())) {
                        String qqurl = shareBean.getQqUrl();
                        if (StringUtils.isNotEmpty(qqurl)) {
                            if (qqurl.contains("?")) {
                                qqurl = qqurl + "&t=" + System.currentTimeMillis();
                            } else {
                                qqurl = qqurl + "?t=" + System.currentTimeMillis();
                            }
                            paramsToShare.setTitleUrl(qqurl);

                        }
                    }


                    int shareMfr = -1;//1 weixin 2微信朋友圈 3qq 4 qq空间 5新浪微博
                    if (platform.getName().equals(Wechat.NAME)) {
                        shareMfr = 1;
                    } else if (platform.getName().equals(WechatMoments.NAME)) {
                        shareMfr = 2;
                    } else if (platform.getName().equals(QQ.NAME)) {
                        shareMfr = 3;
                    } else if (platform.getName().equals(QZone.NAME)) {
                        shareMfr = 4;
                    } else if (platform.getName().equals(SinaWeibo.NAME)) {
                        shareMfr = 5;
                    }
                    StatFactory.getInstance().sendShare(context,shareBean.getGameId(),shareBean.getFrom()+"",shareBean.getFromName());
//                    StatFactory.getInstance(context).sendShareChannel(uid, from, shareMfr, shareBean.getGameId(), shareBean.getArticleId());
                }
            });
            setShareCallback(context,oks,from,weichaurl,shareBean.getGameId());
            // 启动分享GUI
            oks.show(context);

//            StatFactory.getInstance(context).sendStartShare(uid, from);
        } catch (Exception e) {
            e.printStackTrace();
            ToastUtils.getInstance(context).showToastSystem("分享失败。");

        }
    }

    /**
     * 分享
     *
     * @param context
     */
    public static void shareUtilUseFragment(final Context context, final ShareBean shareBean, final ShareContentCustomizeCallback callback) {
        try {
            final String uid = AccountUtil.getInstance().getUid()+"";
            final int from = shareBean.getFrom();
//            ShareSDK.initSDK(context);
            OnekeyShare oks = new OnekeyShare();
//            oks.setDialogMode();
            //关闭sso授权
            oks.disableSSOWhenAuthorize();
            oks.setTitle(shareBean.getTitle());
            oks.setText(shareBean.getText());
            final String imageUrl = shareBean.getImageUrl();
            if (StringUtils.isNotEmpty(imageUrl))
                oks.setImageUrl(imageUrl);
            // url仅在微信（包括好友和朋友圈）中使用
            String weichaurl = shareBean.getWechatShareUrl();
            if (StringUtils.isNotEmpty(weichaurl)) {
                if (weichaurl.contains("?")) {
                    weichaurl = weichaurl + "&t=" + System.currentTimeMillis();
                } else {
                    weichaurl = weichaurl + "?t=" + System.currentTimeMillis();
                }
                oks.setUrl(weichaurl);
            }
            // comment是我对这条分享的评论，仅在人人网和QQ空间使用
            oks.setComment(shareBean.getText());
            // site是分享此内容的网站名称，仅在QQ空间使用
            oks.setSite(context.getString(R.string.app_name));
            // siteUrl是分享此内容的网站地址，仅在QQ空间使用
            oks.setShareContentCustomizeCallback(new ShareContentCustomizeCallback() {
                @Override
                public void onShare(
                        Platform platform, Platform.ShareParams paramsToShare) {
                    if (SinaWeibo.NAME.equals(platform.getName())) {
                        String weibourl = shareBean.getWeiboShareUrl();
                        if (StringUtils.isNotEmpty(weibourl)) {
                            if (weibourl.contains("?")) {
                                weibourl = weibourl + "&t=" + System.currentTimeMillis();
                            } else {
                                weibourl = weibourl + "?t=" + System.currentTimeMillis();
                            }
                            paramsToShare.setTitleUrl(weibourl);
                        }
                        paramsToShare.setText(shareBean.getText() + "\n" + shareBean.getWeiboShareUrl());
                    }
                    if (QZone.NAME.equals(platform.getName())) {
                        String qqzoneurl = shareBean.getqZoneShareUrl();
                        if (StringUtils.isNotEmpty(qqzoneurl)) {
                            if (qqzoneurl.contains("?")) {
                                qqzoneurl = qqzoneurl + "&t=" + System.currentTimeMillis();
                            } else {
                                qqzoneurl = qqzoneurl + "?t=" + System.currentTimeMillis();
                            }
                            paramsToShare.setTitleUrl(qqzoneurl);
                        }
                        String ImageUrl = shareBean.getImageUrl();
                        if (StringUtils.isNotEmpty(ImageUrl))
                            paramsToShare.setImageUrl(ImageUrl);
                    }

                    //qq
                    if (QQ.NAME.equals(platform.getName())) {
                        String qqurl = shareBean.getQqUrl();
                        if (StringUtils.isNotEmpty(qqurl)) {
                            if (qqurl.contains("?")) {
                                qqurl = qqurl + "&t=" + System.currentTimeMillis();
                            } else {
                                qqurl = qqurl + "?t=" + System.currentTimeMillis();
                            }
                            paramsToShare.setTitleUrl(qqurl);

                        }
                    }


                    int shareMfr = -1;//1 weixin 2微信朋友圈 3qq 4 qq空间 5新浪微博
                    if (platform.getName().equals(Wechat.NAME)) {
                        shareMfr = 1;
                    } else if (platform.getName().equals(WechatMoments.NAME)) {
                        shareMfr = 2;
                    } else if (platform.getName().equals(QQ.NAME)) {
                        shareMfr = 3;
                    } else if (platform.getName().equals(QZone.NAME)) {
                        shareMfr = 4;
                    } else if (platform.getName().equals(SinaWeibo.NAME)) {
                        shareMfr = 5;
                    }

//                    StatFactory.getInstance(context).sendShareChannel(uid, from, shareMfr, shareBean.getGameId(), shareBean.getArticleId());

                    if (callback != null) {
                        callback.onShare(platform, paramsToShare);
                    }
                }
            });
            setShareCallback(context,oks,from,weichaurl,shareBean.getGameId());
            // 启动分享GUI
            oks.show(context);

//            StatFactory.getInstance(context).sendStartShare(uid, from);
        } catch (Exception e) {
            e.printStackTrace();
            ToastUtils.getInstance(context).showToastSystem("分享失败。");

        }
    }

    /**
     * @param context
     * @param shareTo   =0 微信好友 ，=1 //微信朋友圈，=2  //qq，=3 //qq空间，=4//新浪微博
     * @param shareBean
     */
    public static void shareToOne(Context context, int shareTo, ShareBean shareBean) {

        final String uid = AccountUtil.getInstance().getUid()+"";
        final int from = (shareBean != null ? shareBean.getFrom() : 0);
        int shareMfr = -1;//1 weixin 2微信朋友圈 3qq 4 qq空间 5新浪微博
        if (shareTo == 0) {
            shareMfr = 1;
        } else if (shareTo == 1) {
            shareMfr = 2;
        } else if (shareTo == 2) {
            shareMfr = 3;
        } else if (shareTo == 3) {
            shareMfr = 4;
        } else if (shareTo == 4) {
            shareMfr = 5;
        }
//        StatFactory.getInstance(context).sendStartShare(uid, from);


//        shareBean.setText("文本正");
//        shareBean.setTitle("标题文本");
//        shareBean.setImageUrl("http://himg2.huanqiu.com/attachment2010/2015/0804/20150804085824692.jpg");
//        shareBean.setQqUrl("https://www.baidu.com/");
//        shareBean.setWechatShareUrl("https://www.baidu.com/");
//        shareBean.setWechatFriendUrl("https://www.baidu.com/");
//        shareBean.setWeiboShareUrl("https://www.baidu.com/");
//        shareBean.setqZoneShareUrl("https://www.baidu.com/");
        //微信好友
        if (shareTo == 0) {
//            ShareSDK.initSDK(context, "563efaa5a8fe");
            HashMap<String, Object> hashMap = new HashMap<String, Object>();
            hashMap.put("Id", "4");
            hashMap.put("SortId", "1");
            hashMap.put("AppId", "wxfe9e973f1c77f234");
            hashMap.put("AppSecret", "fdc1f62a15de7f2024d69bedc0b8c094");
            hashMap.put("BypassApproval", "false");
            hashMap.put("Enable", "true");
            ShareSDK.setPlatformDevInfo(Wechat.NAME, hashMap);
            Wechat.ShareParams weichatsp = new Wechat.ShareParams();
            weichatsp.setShareType(Platform.SHARE_WEBPAGE);
            weichatsp.setTitle(shareBean.getTitle());
            weichatsp.setText(shareBean.getText());
            weichatsp.setImageUrl(shareBean.getImageUrl());
            weichatsp.setUrl(shareBean.getWechatShareUrl());
            Platform weichat = ShareSDK.getPlatform(Wechat.NAME);
            setShareCallback(context, weichat, from,shareBean.getWechatShareUrl());
            // 执行图文分享
            weichat.share(weichatsp);

//            StatFactory.getInstance(context).sendShareChannel(uid, from, shareMfr, shareBean.getGameId(), shareBean.getArticleId());
            return;
        }


//        ShareSDK.initSDK(context);

        switch (shareTo) {
            //微信好友
            case 0:
                Wechat.ShareParams weichatsp = new Wechat.ShareParams();
                weichatsp.setShareType(Platform.SHARE_WEBPAGE);
                weichatsp.setTitle(shareBean.getTitle());
                weichatsp.setText(shareBean.getText());
                weichatsp.setImageUrl(shareBean.getImageUrl());
                weichatsp.setUrl(shareBean.getWechatShareUrl());
                Platform weichat = ShareSDK.getPlatform(Wechat.NAME);
                setShareCallback(context, weichat, from,shareBean.getWechatShareUrl());
// 执行图文分享
                weichat.share(weichatsp);
                break;
            //微信朋友圈
            case 1:
                WechatMoments.ShareParams weichatFsp = new WechatMoments.ShareParams();
                weichatFsp.setShareType(Platform.SHARE_WEBPAGE);
                weichatFsp.setTitle(shareBean.getTitle());
                weichatFsp.setText(shareBean.getText());
                weichatFsp.setImageUrl(shareBean.getImageUrl());
                weichatFsp.setUrl(shareBean.getWechatFriendUrl());
                Platform weichatF = ShareSDK.getPlatform(WechatMoments.NAME);
                setShareCallback(context, weichatF, from,shareBean.getWechatFriendUrl());
// 执行图文分享
                weichatF.share(weichatFsp);
                break;
            //qq
            case 2:
                QQ.ShareParams qqsp = new QQ.ShareParams();
                qqsp.setTitle(shareBean.getTitle());
                qqsp.setTitleUrl(shareBean.getQqUrl()); // 标题的超链接
                qqsp.setText(shareBean.getText());
                qqsp.setImageUrl(shareBean.getImageUrl());
                qqsp.setSite("一起玩");
//                qqsp.setSiteUrl("发布分享网站的地址");

                Platform qq = ShareSDK.getPlatform(QQ.NAME);
                setShareCallback(context, qq, from,shareBean.getQqUrl());
// 执行图文分享
                qq.share(qqsp);
                break;
            //qq空间
            case 3:
                QZone.ShareParams qqZonesp = new QZone.ShareParams();
                qqZonesp.setTitle(shareBean.getTitle());
                qqZonesp.setTitleUrl(shareBean.getqZoneShareUrl()); // 标题的超链接
                qqZonesp.setText(shareBean.getText());
                qqZonesp.setImageUrl(shareBean.getImageUrl());
                qqZonesp.setSite("一起玩");
//                qqZonesp.setSiteUrl("发布分享网站的地址");

                Platform qzone = ShareSDK.getPlatform(QZone.NAME);
                setShareCallback(context, qzone, from,shareBean.getqZoneShareUrl());
// 执行图文分享
                qzone.share(qqZonesp);
                break;
            //新浪微博
            case 4:
                SinaWeibo.ShareParams sinasp = new SinaWeibo.ShareParams();
                sinasp.setText(shareBean.getText() + shareBean.getWeiboShareUrl());
                sinasp.setImageUrl(shareBean.getImageUrl());

                Platform weibo = ShareSDK.getPlatform(SinaWeibo.NAME);
                setShareCallback(context, weibo, from,shareBean.getWeiboShareUrl());
// 执行图文分享
                weibo.share(sinasp);
                break;
            case 5:

                break;
        }


//        StatFactory.getInstance(context).sendShareChannel(uid, from, shareMfr, shareBean.getGameId(), shareBean.getArticleId());
    }

    /**
     * @param context
     * @param shareTo   =0 微信好友 ，=1 //微信朋友圈，=2  //qq，=3 //qq空间，=4//新浪微博
     * @param message   分享内容
     * @param shareType 分享类型 0默认, 1文字 ， 2 图片
     */
    public static void shareToOne(Context context, int shareTo, String message, int shareType, int from) {
        final String uid = AccountUtil.getInstance().getUid()+"";
        int shareMfr = -1;
        if (shareTo == 0) {
            shareMfr = 1;
        } else if (shareTo == 1) {
            shareMfr = 2;
        } else if (shareTo == 2) {
            shareMfr = 3;
        } else if (shareTo == 3) {
            shareMfr = 4;
        } else if (shareTo == 4) {
            shareMfr = 5;
        }

        switch (shareType) {
            case 0:
                try {
                    ShareBean shareBean = JsonMapper.getInstance().fromJson(message, ShareBean.class);
                    shareBean.setFrom(from);
                    shareToOne(context, shareTo, shareBean);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case 2:
//                StatFactory.getInstance(context).sendStartShare(uid, from);

                switch (shareTo) {
                    case 0:
                        shareWechatImage(context, message, from);
                        break;
                    case 1:
                        shareWechatPlantformImage(context, message, from);
                        break;
                    case 2:
                        shareQQImage(context, message, from);
                        break;
                    case 3:
                        break;
                    case 4:
                        shareWeiboImage(context, message, from);
                        break;
                }

//                StatFactory.getInstance(context).sendShareChannel(uid, from, shareMfr, "0", "0");
                break;
            case 1:
//                StatFactory.getInstance(context).sendStartShare(uid, from);

                switch (shareTo) {
                    case 0:
                        shareWechatText(context, message, from);
                        break;
                    case 1:
                        shareWechatPlantformText(context, message, from);
                        break;
                    case 2:
                        shareQQText(context, message, from);
                        break;
                    case 3:
                        break;
                    case 4:
                        shareWeiboText(context, message, from);
                        break;
                }

//                StatFactory.getInstance(context).sendShareChannel(uid, from, shareMfr, "0", "0");
                break;
        }

    }

    private static void shareQQText(final Context context, String message, final int from) {
//        ShareSDK.initSDK(context);
        QQ.ShareParams qqsp = new QQ.ShareParams();
        qqsp.setText(message);
        Platform qq = ShareSDK.getPlatform(QQ.NAME);
        setShareCallback(context, qq, from,message);
        qq.share(qqsp);
    }

    private static void shareQQImage(final Context context, String imageUrl, final int from) {
//        ShareSDK.initSDK(context);
        QQ.ShareParams qqsp = new QQ.ShareParams();
        qqsp.setImageUrl(imageUrl);
        Platform qq = ShareSDK.getPlatform(QQ.NAME);
        setShareCallback(context, qq, from,imageUrl);
        qq.share(qqsp);
    }

    private static void shareWeiboText(final Context context, String message, final int from) {
//        ShareSDK.initSDK(context);
        SinaWeibo.ShareParams weiBo = new SinaWeibo.ShareParams();
        weiBo.setText(message);
        Platform weiBoPlat = ShareSDK.getPlatform(SinaWeibo.NAME);
        setShareCallback(context, weiBoPlat, from,message);
        weiBoPlat.share(weiBo);
    }

    private static void shareWeiboImage(final Context context, String imageUrl, final int from) {
//        ShareSDK.initSDK(context);
        SinaWeibo.ShareParams weiBo = new SinaWeibo.ShareParams();
        weiBo.setText("分享");
        weiBo.setImageUrl(imageUrl);
        Platform weiBoPlat = ShareSDK.getPlatform(SinaWeibo.NAME);
        setShareCallback(context, weiBoPlat, from,imageUrl);
        weiBoPlat.share(weiBo);
    }

    private static void shareWechatText(final Context context, String message, final int from) {
//        ShareSDK.initSDK(context, "563efaa5a8fe");
        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        hashMap.put("Id", "4");
        hashMap.put("SortId", "1");
        hashMap.put("AppId", "wxfe9e973f1c77f234");
        hashMap.put("AppSecret", "fdc1f62a15de7f2024d69bedc0b8c094");
        hashMap.put("BypassApproval", "false");
        hashMap.put("Enable", "true");
        ShareSDK.setPlatformDevInfo(Wechat.NAME, hashMap);
        Wechat.ShareParams weichat = new Wechat.ShareParams();
        weichat.setShareType(Platform.SHARE_TEXT);
        weichat.setTitle("分享");
        weichat.setText(message);
        Platform weichatPlat = ShareSDK.getPlatform(Wechat.NAME);
        setShareCallback(context, weichatPlat, from,message);
        weichatPlat.share(weichat);
    }

    private static void shareWechatImage(final Context context, String imageUrl, final int from) {
//        ShareSDK.initSDK(context, "563efaa5a8fe");
        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        hashMap.put("Id", "4");
        hashMap.put("SortId", "1");
        hashMap.put("AppId", "wxfe9e973f1c77f234");
        hashMap.put("AppSecret", "fdc1f62a15de7f2024d69bedc0b8c094");
        hashMap.put("BypassApproval", "false");
        hashMap.put("Enable", "true");
        ShareSDK.setPlatformDevInfo(Wechat.NAME, hashMap);
        Wechat.ShareParams weichat = new Wechat.ShareParams();
        weichat.setShareType(Platform.SHARE_IMAGE);
        weichat.setTitle("分享");
        weichat.setImageUrl(imageUrl);
        Platform weichatPlat = ShareSDK.getPlatform(Wechat.NAME);
        setShareCallback(context, weichatPlat, from,imageUrl);
        weichatPlat.share(weichat);
    }

    private static void shareWechatPlantformImage(final Context context, String imageUrl, final int from) {
//        ShareSDK.initSDK(context);
        WechatMoments.ShareParams weichat = new WechatMoments.ShareParams();
        weichat.setShareType(Platform.SHARE_IMAGE);
        weichat.setTitle("分享");
        weichat.setImageUrl(imageUrl);
        Platform weichatPlat = ShareSDK.getPlatform(WechatMoments.NAME);
        setShareCallback(context, weichatPlat, from,imageUrl);
        weichatPlat.share(weichat);
    }

    private static void shareWechatPlantformText(final Context context, String message, final int from) {
//        ShareSDK.initSDK(context);
        WechatMoments.ShareParams weichat = new WechatMoments.ShareParams();
        weichat.setShareType(Platform.SHARE_TEXT);
        weichat.setTitle("分享");
        weichat.setText(message);
        Platform weichatPlat = ShareSDK.getPlatform(WechatMoments.NAME);
        setShareCallback(context, weichatPlat, from,message);
        weichatPlat.share(weichat);
    }

    public static void setShareCallback(final Context context, Platform platform, final int from, final String url) {
        if (platform == null) return;
        try {
            platform.setPlatformActionListener(new PlatformActionListener() {
                @Override
                public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                    sendShareResult(context, platform, from, 2,url,"");
                }

                @Override
                public void onError(Platform platform, int i, Throwable throwable) {
                    ToastUtils.getInstance(context).showToastSystem("分享失败");
                    sendShareResult(context, platform, from, 3,url,"");
                }

                @Override
                public void onCancel(Platform platform, int i) {
                    sendShareResult(context, platform, from, 1,url,"");
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void setShareCallback(final Context context, OnekeyShare oks, final int from, final String url, final String gameid) {
        if (oks == null) return;
        try {
            oks.setCallback(new PlatformActionListener() {
                @Override
                public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                    sendShareResult(context, platform, from, 2,url,gameid);
                }

                @Override
                public void onError(Platform platform, int i, Throwable throwable) {
                    ToastUtils.getInstance(context).showToastSystem("分享失败");
                    sendShareResult(context, platform, from, 3,url,gameid);
                }

                @Override
                public void onCancel(Platform platform, int i) {
                    sendShareResult(context, platform, from, 1,url,gameid);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private static void sendShareResult(final Context context, Platform platform, int from, int result, String url, String gameid) {
        //1取消 2成功 3失败
        final String uid = AccountUtil.getInstance().getUid()+"";

        int shareMfr = -1;//1 weixin 2微信朋友圈 3qq 4 qq空间 5新浪微博
        String shareChannel = "";//1 weixin 2微信朋友圈 3qq 4 qq空间 5新浪微博
        if (platform.getName().equals(Wechat.NAME)) {
            shareMfr = 1;
            shareChannel="weixin";
        } else if (platform.getName().equals(WechatMoments.NAME)) {
            shareMfr = 2;
            shareChannel="quan";
        } else if (platform.getName().equals(QQ.NAME)) {
            shareMfr = 3;
            shareChannel="qq";
        } else if (platform.getName().equals(QZone.NAME)) {
            shareMfr = 4;
            shareChannel="qzone";
        } else if (platform.getName().equals(SinaWeibo.NAME)) {
            shareMfr = 5;
            shareChannel="weibo";
        }

        if (result==2 || (platform.getName().equals(QQ.NAME) && result!=3)) {
            long nowTime = PreferenceUtil.getLongPreferences(context, PreferenceUtil.PRE_FIRST_SHARE);
            AccountBean mAccountBean = AccountUtil.getInstance().getAccountData();
            if (!DateUtils.isToday(nowTime) && (
                    (mAccountBean==null) || (mAccountBean!=null && mAccountBean.getShareMoney()>0))) {
//                MessageMoneyDialog mMessageMoneyDialog = new MessageMoneyDialog(context);
//                mMessageMoneyDialog.show();
//                mMessageMoneyDialog.setShowType(1);
            }

            MobileCodeUtils.taskShareTag(context);
        }

//        StatFactory.getInstance(context).sendShareResult(uid, from, shareMfr, result);
//        SharePostRequest request=new SharePostRequest();
//        request.setUid(uid);
//        //result 分享结果 1 取消  2 成功  3失败
//        request.setStatus(result);
//        request.setShare_url(url);
//        //from;//1，APP分享；2，游戏详情分享；3，资讯分享；4，社区分享；5，活动分享（H5）;6.直播
//        request.setRes_id(gameid);
//        request.setRes_type(from);
//        request.setShare_channel(shareChannel);
//        CommonService_.intent(context).extra("sharePostData",request).start();
    }

    //endregion
}
